package edu.chalmers;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class CollectionFragment extends Fragment{
	private BookManager bookManager = SharedBookManager.getBookManager();
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState){
		return inflater.inflate(R.layout.collection, container, false);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		View view = getView();
		
		ListView listView = (ListView) view.findViewById(R.id.bookList);
        listView.setAdapter(new ArrayAdapter<Book>(view.getContext(), android.R.layout.simple_list_item_1, bookManager.allBooks()));
        
        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Intent intent = new Intent(view.getContext(), BookDetailActivity.class);
            	intent.putExtra(Book.TITLE, position);
            	view.getContext().startActivity(intent);
            }
        });
	}
}